package org.arkeologgr.appening.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Singleton;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.arkeologgr.appening.entity.Item;
import org.arkeologgr.appening.entity.ItemComment;
import org.arkeologgr.appening.entity.ItemDetails;
import org.arkeologgr.appening.entity.ItemImage;
import org.arkeologgr.appening.entity.ServiceUser;


@Singleton
public class InternalAdapter {

	@PersistenceContext
	private EntityManager em;
	
	public static InternalAdapter lookup() {
		try {
			return (InternalAdapter) new InitialContext().lookup("java:global/arkeologgr/InternalAdapter");
		} catch (NamingException e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<Item> getArea(double lat1, double lon1, double lat2, double lon2) {
		
		// List<Item> internalItems = findItems(ksamItems);
		// return ksamItems;
		
		System.out.println("lat1 = " + lat1 + ", lon1 = " + lon1);
		System.out.println("lat2 = " + lat2 + ", lon2 = " + lon2);
		
		TypedQuery<Item> tq = em.createQuery("select i from Item i",Item.class);
		List<Item> items = tq.getResultList();
		List<Item> itemList = new ArrayList<Item>();
		for (Item it : items) {
			System.out.println("lat = " + it.getLatitude() + ", lon = " + it.getLongitude());
			if (it.getLatitude() >= lat1 && it.getLatitude() <= lat2 && it.getLongitude() >= lon1 && it.getLongitude() <= lon2) {
				itemList.add(it);
			}
		}
		return itemList;
	}
	
	public void addItem(Item item) {
		em.persist(item);
		em.flush();
	}

	public List<ItemComment> findComments(Item item) {
		TypedQuery<ItemComment> tq = em.createQuery("select c from ItemComment c where c.entityUri = :uri", ItemComment.class);
		tq.setParameter("uri", item.getEntityUri());
		return tq.getResultList();
	}
	
	/*
	private List<Item> findItems(List<Item> ksamItems) {
		List<String> entityUriList = new ArrayList<String>();
		for (Item it : ksamItems) {
			// entityUriList.add(it.getEntityUri());
			// Item internalItem = em.find(Item.class, it.getEntityUri());
			List<ItemComment> comments = findComments(it);			
		}
		TypedQuery<Item> tq = em.createQuery("select i from Item i where i.entityUri in :ids",Item.class);
		tq.setParameter("ids", entityUriList);
		return tq.getResultList();
	}
	*/
	

	public ItemImage getImage(long imageId) {
		return em.find(ItemImage.class,imageId);
	}

	public ServiceUser getUser(String userName) {
		System.out.println("Searching for user = " + userName);
		TypedQuery<ServiceUser> tq = em.createQuery("select u from ServiceUser u where u.userName = :name",ServiceUser.class);
		tq.setParameter("name", userName);
		return tq.getSingleResult();
	}

	public void addComment(ItemComment entity) {
		em.persist(entity);
		em.flush();
	}

	public void addItemDetails(ItemDetails itemDetails) {
		em.persist(itemDetails);
		em.flush();
	}

	public void addItemImage(ItemImage itemImage) {
		em.persist(itemImage);
		em.flush();
	}

	public List<ItemImage> findImages(Item item) {
		TypedQuery<ItemImage> tq = em.createQuery("select i from ItemImage i where i.entityUri = :uri",ItemImage.class);
		tq.setParameter("uri", item.getEntityUri());
		return tq.getResultList();
	}

	public ItemDetails getItemDetails(String entityUri) {
		TypedQuery<ItemDetails> tq = em.createQuery("select id from ItemDetails id where id.item.entityUri = :uri",ItemDetails.class);
		tq.setParameter("uri", entityUri);
		try {
			ItemDetails id = tq.getSingleResult();
			return id;
		} catch (NoResultException e) {
		}
		return null;
	}
	

}
